package com.zlsy.secondtime.threadcoreknowledge.threadobjectclasscommonmethods;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouliang
 * @date 2020/5/18 8:58
 * <p>
 * 每隔一秒输出当前的时间，观察被中断的情况。
 */
public class SleepInterrupted implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("当前时间为:" + (new Date()));
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("线程被中断了。。");
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SleepInterrupted r = new SleepInterrupted();
        Thread thread = new Thread(r);
        thread.start();

        TimeUnit.SECONDS.sleep(5);
        thread.interrupt();
    }
}
